package com.yjd.api.user;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.credit.model.CreditLineApplyModel;
import com.yjd.comm.credit.service.ICreditLineApplyService;
import com.yjd.comm.enums.OrderStatusEnum;
import com.yjd.comm.system.model.SysParamModel;
import com.yjd.comm.system.util.SysParamUtil;
import com.yjd.comm.user.dto.UserSession;
import com.yjd.comm.user.model.UserDataModel;
import com.yjd.comm.user.service.IUserDataService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.vo.Constant;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by yangchangyan on 2017/6/28.
 */
@Controller("com.yjd.api.user.CreditLineControl")
@Scope("prototype")
@RequestMapping("/credit")
public class CreditLineControl extends BaseControl {

    @Resource
    ICreditLineApplyService creditLineApplyService;

    @Resource
    IUserDataService userDataService;

    @RequestMapping("/info")
    public void userCredit() throws Exception {
        UserSession user = this.getUserSession();
        SysParamModel sysParam = SysParamUtil.getSysParamService().getModelOne(
                FrameUtil.newHashMap("param_key", Constant.SysParamKeyEnum.CREDIT_LIMIT_MAX.getValue()), DbWREnums.READ);
        if (null == sysParam) {
            this.errorMsg("查询失败，稍后重试。");
            return;
        }

        UserDataModel uData = userDataService.getModelOne(FrameUtil.newHashMap("user_id", user.getUser_id()), DbWREnums.READ);

        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        paramMap.put("sysMaxCreditLine", NumberUtil.formatNumber(Double.valueOf(sysParam.getParam_value())));
        paramMap.put("creditLine", NumberUtil.formatNumber(uData.getCredit_line()));
        paramMap.put("residualAmount", NumberUtil.formatNumber(uData.getResidual_amount()));
        this.successData(paramMap);
    }

    /**
     * 信用额度申请
     * Created by yangchangyan on 2017/6/28.
     *
     * @param applyAmount 申请的信用额度金额需要和系统配置的梯度金额一直
     * @return void
     * @throws
     */
    @RequestMapping("/apply")
    public void userApply(Double applyAmount) throws Exception {

        if (null == applyAmount || applyAmount < 0) {
            FrameUtil.throwBaseException1("参数错误，请重试。");
        }

        // 获取系统信用额度配置信息
        SysParamModel sysParam = SysParamUtil.getSysParamModel(Constant.SysParamKeyEnum.CREDIT_LIMIT_MAX);
        if (null == sysParam) {
            FrameUtil.throwBaseException1("查询失败，稍后重试。");
        }

        Double sysMaxCreditLint = Double.valueOf(sysParam.getParam_value());

        UserSession user = this.getUserSession();
        UserDataModel uData = userDataService.getModelOne(FrameUtil.newHashMap("user_id", user.getUser_id()), DbWREnums.READ);

        // 申请的信用额度大于系统最大额度 或者 小于当前用户额度 为非法情况
        if (applyAmount > sysMaxCreditLint || applyAmount < uData.getCredit_line()) {
            FrameUtil.throwBaseException1("参数错误，请重试。");
        }

        // 如果已经是最大限额，则不能再申请
        if (uData.getCredit_line() == sysMaxCreditLint) {
            this.successMsg("您的信用额度已经是最大的了");
            return;
        }
        // 创建额度申请订单
        CreditLineApplyModel clam = new CreditLineApplyModel();
        clam.setUser_id(user.getUser_id());
        clam.setApply_credit_line(applyAmount);
        clam.setApply_time(FrameUtil.getTime());
        clam.setStatus(OrderStatusEnum.INIT.getCode());
        clam.setLast_update_time(FrameUtil.getTime());
        creditLineApplyService.insert(clam);
        this.successMsg("额度申请已提交，请等待审批。");
    }
}
